package com.xiaoyu.izf.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import com.xiaoyu.izf.aspect.RequestAuth;
import com.xiaoyu.izf.model.GoodsInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.FormResultBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/goods")

public class GoodsController extends BaseController{
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/list_page")
    @RequestAuth
    public String listPage() {
        return "mgr/goods/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    @RequestAuth
    public DataTableBean list(HttpServletRequest request, @RequestBody String body) {
        ParamListBean paramListBean = getParamListBean(body);
        System.out.println(paramListBean.getFieldName());
        System.out.println(paramListBean.getCompositorType());
        DataTableBean dataTableBean = goodsService.list(paramListBean);
        return dataTableBean;
    }

    @RequestMapping("/go_top")
    @ResponseBody
    @RequestAuth
    public FormResultBean goTop(HttpServletRequest request,int id) {
        FormResultBean fr = new FormResultBean();
        goodsService.goTop(id);
        fr.setType("success");
        fr.setMessage("操作成功。");
        return fr;
    }

    @RequestMapping("/del")
    @ResponseBody
    @RequestAuth
    public FormResultBean del(HttpServletRequest request,int id) {
        FormResultBean fr = new FormResultBean();
        goodsService.goodsInfoDel(id);
        fr.setType("success");
        fr.setMessage("操作成功。");
        return fr;
    }

    /**
     * 价格修改
     * @return
     */
    @RequestMapping("/price_mod_page")
    @RequestAuth
    public String priceModPage(int id, Model model) {
        GoodsInfo goodsInfo = goodsService.oneById(id);
        model.addAttribute("goodsInfo",goodsInfo);
        return "mgr/goods/price_mod";
    }

    @RequestMapping("/price_mod")
    @ResponseBody
    @RequestAuth()
    public FormResultBean priceMod(HttpServletRequest request, int id,double price,double viewPrice) {
        GoodsInfo goodsInfo = goodsService.oneById(id);
        goodsInfo.setPrice(Convert.toInt(price*100));
        goodsInfo.setViewPrice(Convert.toInt(viewPrice*100));
        goodsService.save(goodsInfo);
        FormResultBean fr = new FormResultBean();
        return fr.setType("success").setMessage("操作成功。");
    }
}
